package com.cskaoyan.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.dto.UserLoginResponse;
import com.cskaoyan.user.form.KaptchaCodeRequest;
import com.cskaoyan.user.form.UserLoginRequest;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author : qj
 * @since: 2022/10/26  10:31
 */
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private IKaptchaService iKaptchaService;

    @Autowired
    private ILoginService loginService;

    @PostMapping("/login")
    public ResponseData login(@RequestBody Map<String, String> map, HttpServletRequest request, HttpServletResponse response) {
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String captcha = map.get("captcha");

        //验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);
        try {
            iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        } catch (Exception e) {
            String message = e.getMessage();
            return new ResponseUtil<>().setErrorMsg(message);
        }

        //验证用户名和密码
        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setUserName(userName);
        userLoginRequest.setUserPwd(userPwd);
        UserLoginResponse userLoginResponse = loginService.login(userLoginRequest);

        //说明验证用户名和密码成功
        if (userLoginResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            Cookie cookie = CookieUtil.genCookie("access_token", userLoginResponse.getToken(), "/", -1);
            CookieUtil.setCookie(response, cookie);
            return new ResponseUtil<>().setData(userLoginResponse);
        }
        return new ResponseUtil<>().setErrorMsg(userLoginResponse.getMsg());
    }

    @GetMapping("/login")
    public ResponseData checkLogin(HttpServletRequest request, HttpServletResponse response) {
        String userInfo = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        return new ResponseUtil<>().setData(object);
    }

    @GetMapping("/loginOut")
    public ResponseData loginOut(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("access_token".equals(cookie.getName())) {
                    cookie.setPath("/");
                    cookie.setMaxAge(0);
                    CookieUtil.setCookie(response, cookie);
                }
            }
        }
        return new ResponseUtil<>().setData(null,"success");
    }
}
